/**
 *消息发送框  
 */
var MsgEditor = (function() {    
    function _msgEditor(chatPanel) {
        var _this = this;
        
        this.isUseTextarea = true;
         
        this.chatPanel = chatPanel;
        this.$wrapper = chatPanel.getMsgboxPlaceholder();
        
        if(this.isUseTextarea){  //普通文本域.
            this.$chatEditorBox = this.$wrapper.closest('.chatEditorBox');
            this.$chatEditorBox.attr('contenteditable', false);
            this.$textarea = $('<textarea style="width: 100%;height: 100%;"></textarea>');
            this.$chatEditorBox.append(this.$textarea);
            this.$textarea.keydown(function(e) {
                if (e.keyCode === 13) {                    
                    var flag = _this.chatPanel.msgEditorToolbar.toolbar.getValues()['sendActionType'];                    
                    if ((flag && e.ctrlKey) || (!flag && !e.ctrlKey)) {
                        _this.send();                        
                    } else {
                        _this.$textarea.insertTextByCursor('\n');
                    }
                    return false;
                }
                if (e.keyCode === 9) {
                    _this.$textarea.insertTextByCursor('\t');
                    return false;
                }
            });
            this.$textarea.on('paste', function(e) {                
                var clipboard = e.originalEvent.clipboardData;
                var items = [];
                if (clipboard.items) {  
                    var i = 0;
                    while (i < clipboard.items.length) {
                        var item = clipboard.items[i];
                        if(item.kind == 'file'){
                            var val = item.getAsFile();
                            items.push(val);
                        }
                        i++;
                    }    
                }
                if(items.length){
                    ujs.ui.Dialog.get({
                        title : ' Send screenshot from clipboard',
                        width : '80%',
                        height : '80%',
                        bodyStyle : 'text-align:center',
                        html : '<img src="{0}" />'.format(URL.createObjectURL(items[0])),
                        buttons : [
                            {   
                                props : 'Send',    
                                handler : function($btnObj){
                                    _this.chatPanel.msgEditorToolbar.upfiles(items);
                                    this.close();                
                                }
                            }, {   
                                props : 'Cancel',    
                                handler : function($btnObj){
                                    this.close();
                                }
                            }
                        ]
                    });
                    return false;
                }                
            }); 
        } else {    //富文本编辑.
            this.$chatEditorBox.attr('contenteditable', true);
            this.$wrapper.keydown(function(e) {
                if (e.keyCode === 13) {
                    if (!e.ctrlKey) {
                        _this.send();
                        return false;
                    } else {
                        document.execCommand('insertHTML', false, '<div><br/></div>');
                    }
                }
            });
            //仅允许paste纯文本.
            this.$wrapper.onlyPastePlainText();             
        }
                      
        //添加托拽上传区.
        ujs.browser.dd.enableDragDropZone( _this.$wrapper, {
            dragenter : function(){
                bubble.msg(lan('dragDrop.dropFileTip'));
            },
            dragleave : function(){
                bubble.hide();
            },
            drop : function(e){
                //Folder上传.
                _this.chatPanel.msgEditorToolbar.upfiles(Array.toArray(e.originalEvent.dataTransfer.files));                
                bubble.hide();                    
            }
        });
    }

    /**
     * 获取输入的内容(Html格式).
     * 若输入框中仅存在空字符或空标记，则返回空字符串. 
     */
    _msgEditor.prototype.getValue = function() {     
        if(this.isUseTextarea){
            return this.$textarea.val();
        } else {
            return this.$wrapper.text().trim() ? this.getHtml() : "";
        }    
    };

    /**
     * 获取输入框中自动生成的html标记. 
     */
    _msgEditor.prototype.getHtml = function() {
        if(this.isUseTextarea){
            return this.$textarea.val();
        } else {
            return this.$wrapper.html();
        }
    };
    
    /**
     * 清空输入框. 
     */
    _msgEditor.prototype.clear = function() {
        if(this.isUseTextarea){
            return this.$textarea.val('');
        } else {
            return this.$wrapper.html('');
        }
    };

    _msgEditor.prototype.send = function() {
        var text = this.getValue().trim();
        if (text) {   
            MsgSender.send('plainText', this.isUseTextarea ? text : text.toPlainText());
            this.clear();
        } else {
            bubble.msg('Please input message.');
        }
    };
    return _msgEditor;
})(); 